﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sac.Data;
using System.Configuration;
using System.Data;

namespace Sac.Web
{
    public class FanCodeToSysCodeOperate
    {
        OledbPro oledb;

        public FanCodeToSysCodeOperate()
        {
            string ConnStr = ConfigurationManager.AppSettings["access"].ToString();
            oledb = new OledbPro(ConnStr);
        }

        /// <summary>
        /// 插入风机状态码对照信息
        /// </summary>
        /// <param name="fan">保护相关信息对象</param>
        /// <returns>操作十分成功</returns>
        public bool Insert(FanCodeToSysCode fan)
        {
            string SqlInsert = "insert into FANCODE_TO_SYSCODE(plant_name,fan_code,fan_desc,sys_code,sys_desc) values('"
                + fan.PlantName + "'," + fan.FanCode + ",'" + fan.FanDesc + "'," + fan.SysCode + ",'" + fan.SysDesc + "')";
            int i = oledb.ExecuteNonQuery(SqlInsert);
            if (i == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除风机状态码对照信息
        /// </summary>
        /// <param name="fan">信息在数据库中的ID_KEY</param>
        /// <returns>操作是否成功</returns>
        public bool DeleteByIdKey(string idKey)
        {
            string SqlDelete = "delete from FANCODE_TO_SYSCODE where id_key=" + idKey;
            int i = oledb.ExecuteNonQuery(SqlDelete);
            if (i == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新风机状态码对照表
        /// </summary>
        /// <param name="fan">保护新的状态对照码的信息</param>
        /// <returns></returns>
        public bool UpdateByIdKey(FanCodeToSysCode fan)
        {
            string SqlUpdate = "update FANCODE_TO_SYSCODE set plant_name='" + fan.PlantName + "',fan_code=" + fan.FanCode
                + ",fan_desc='" + fan.FanDesc + "',sys_code=" + fan.SysCode + ",sys_desc='" + fan.SysDesc + "' where id_key=" + fan.IdKey;
            int i = oledb.ExecuteNonQuery(SqlUpdate);
            if (i == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据电站和风机状态码查找系统状态码表
        /// </summary>
        /// <param name="plantName">场站名</param>
        /// <param name="fanCode">风机状态名</param>
        /// <returns>保护风机状态码对照信息的对象</returns>
        public FanCodeToSysCode GetInfoByFanPlantNameAndFanCode(string plantName, int fanCode)
        {
            FanCodeToSysCode fan = new FanCodeToSysCode();
            string SqlGetInfo = "select * from FANCODE_TO_SYSCODE where plant_name='" + plantName + "' and FAN_CODE=" + fanCode;
            DataRow drInfo = oledb.GetDataRow(SqlGetInfo);
            if (drInfo != null)
            {
                fan.IdKey = Convert.ToInt32(drInfo["ID_KEY"]);
                fan.SysCode = Convert.ToInt32(drInfo["SYS_CODE"]);
                fan.SysDesc = drInfo["SYS_DESC"].ToString();
                fan.PlantName = drInfo["PLANT_NAME"].ToString();
                fan.FanCode = fanCode;
                fan.FanDesc = drInfo["FAN_DESC"].ToString();
            }
            return fan;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="idKey"></param>
        /// <returns></returns>
        public FanCodeToSysCode GetFanInfoByIdKey(string idKey)
        {
            FanCodeToSysCode fan = new FanCodeToSysCode();
            string SqlGetInfo = "select * from FANCODE_TO_SYSCODE where id_key=" + idKey;
            DataRow drInfo = oledb.GetDataRow(SqlGetInfo);
            if (drInfo != null)
            {
                fan.IdKey = Convert.ToInt32(drInfo["ID_KEY"]);
                fan.SysCode = Convert.ToInt32(drInfo["SYS_CODE"]);
                fan.SysDesc = drInfo["SYS_DESC"].ToString();
                fan.PlantName = drInfo["PLANT_NAME"].ToString();
                fan.FanCode = Convert.ToInt32(drInfo["FAN_CODE"]);
                fan.FanDesc = drInfo["FAN_DESC"].ToString();
            }
            return fan;
        }

        /// <summary>
        /// 按场站获取风机状态码对照表信息列表
        /// </summary>
        /// <param name="plantName">场站名</param>
        /// <returns>包含风机状态码对照表信息列表的DataTable对象</returns>
        public DataTable GetFanInfoListByPlantName(string plantName)
        {
            string SqlGetFanList = "select * from FANCODE_TO_SYSCODE where plant_name='" + plantName + "' order by fan_code asc";
            DataTable dtFanList = oledb.GetDataTable(SqlGetFanList);
            return dtFanList;
        }
    }
}
